﻿using Microsoft.AspNetCore.Mvc;
using MonitorCode.Hangfire;
using Support.Models;
using Support.Models.Entities;

namespace DevSupport.Areas.Monitor
{
    [ApiController]
    public class HangfireController : ControllerBase
    {
        private ILogger<HangfireController> _logger;
        private readonly IHangfireService _jobSservice;
        private readonly IMonitorDataService _monitorDataService;

        public HangfireController(ILogger<HangfireController> logger, IHangfireService jobSservice, IMonitorDataService monitorDataService)
        {
            _logger = logger;
            _jobSservice = jobSservice;
            _monitorDataService = monitorDataService;
        }

        /// <summary>
        /// 服务监控列表
        /// </summary>
        /// <param name="keyword">关键字搜索</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/MonitorServer/GetJobList")]
        public async Task<object> GetJobList(string? keyword = "")
        {
            return await _jobSservice.GetJobFireListAsync(keyword);
        }

        /// <summary>
        /// 添加服务hangfirejob
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/MonitorServer/AddJob")]
        public TData AddJob(Job_MonitorServer dto)
        {
            var model = new Job_MonitorServer
            {
                JobId = Guid.NewGuid().ToString(),// + dto.GroupName + dto.ServerName.Trim()
                GroupName = dto.GroupName,
                ServerName = dto.ServerName.Trim(),
                ServerType = dto.ServerType,
                CronExpression = dto.CronExpression,
                CheckPath = dto.CheckPath.Trim(),
                ServerUrl = dto.ServerUrl.Trim(),
                Status = JobFireStatus.No,
            };
            return _jobSservice.AddJobFire(model);
        }

        /// <summary>
        /// 开启服务监控
        /// </summary>
        /// <param name="JobId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/MonitorServer/StartJob")]
        public async Task<TData> StartJobAsync(string JobId)
        {
            return await _jobSservice.StartJobFireAsync(JobId);
        }

        /// <summary>
        /// 停止服务监控
        /// </summary>
        /// <param name="JobId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/MonitorServer/StopJob")]
        public async Task<TData> StopJobAsync(string JobId)
        {
            return await _jobSservice.DeleteJobFireAsync(JobId);
        }
        /// <summary>
        /// 删除服务监控
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Route("api/MonitorData/DeleteServerJob")]
        public async Task<TData> DeleteServerJob(string jobId)
        {
            return await _jobSservice.DeleteServerMonitor(jobId);
        }

        /// <summary>
        /// 获取接收者邮件
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Monitor/GetReceiveEmail")]
        public TData GetReceiveEmail(string jobId)
        {
            return _jobSservice.GetReceiveEmail(jobId);
        }

        /// <summary>
        /// 设置接收者邮件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Monitor/SetReceiveEmail")]
        public TData SetReceiveEmail(ReceiveEmailConfig model)
        {
            return _jobSservice.SetReceiveEmail(model);
        }
    }
}